package by.grsu.hdm;

import java.util.ArrayList;
import java.util.List;

import by.grsu.hdm.devices.MediaCenter;
import by.grsu.hdm.devices.NetworkMonitor;
import by.grsu.hdm.devices.UsbFlashDrive;
import by.grsu.hdm.devices.WashingMachine;
import by.grsu.hdm.exception.EmptyPlayListException;
import by.grsu.hdm.media.AudioFile;
import by.grsu.hdm.media.MediaData;
import by.grsu.hdm.media.RemoteControl;
import by.grsu.hdm.media.VideoFile;
import by.grsu.hdm.model.IDevice;
import by.grsu.hdm.model.IHomeDevice;
import by.grsu.hdm.model.INetworkAvailable;
import by.grsu.hdm.model.IUsbPort;
import by.grsu.hdm.model.IUsbSupport;
import by.grsu.hdm.reporting.TextReportBuilder;
import by.grsu.hdm.reporting.TrafficReportData;

public class HomeDeviceManagerStarter {

	public static void main(String[] args) {
		
		List<IHomeDevice> devices = new ArrayList<IHomeDevice>();
		
		MediaCenter media = new MediaCenter("MEDIA#132456");
		media.getUsbPort().connectDevice(new UsbFlashDrive("FLASH-4GB"));
		devices.add(media);
		devices.add(new WashingMachine("WASHING#45"));
		
		
		System.out.println("__________________________________");
		// turn all devices on..
		for (IHomeDevice device : devices) {
			device.turnOn();
		}
		System.out.println("__________________________________");
		//playing song and video 
		try {
			media.getMediaPlayer().play();
		} catch (EmptyPlayListException e) {
			System.out.println(e.getMessage());
		}
				MediaData data = new MediaData("Song", "Semerikov", "Some data");
				media.getMediaPlayer().addFile(new AudioFile(data));
				data = new MediaData("Video", "Semerikov", "Some data");
				media.getMediaPlayer().addFile(new VideoFile(data));
				RemoteControl control = new RemoteControl();
				control.setDevice(media);
				try {
					control.play();
				} catch (EmptyPlayListException e1) {
					System.out.println(e1.getMessage());
				}
				System.out.println("___________with control");
				try {
					media.getMediaPlayer().play();
				} catch (EmptyPlayListException e) {
					System.out.println(e.getMessage());
				}
			
				System.out.println("__________________________________");
		// check for connected devices
		for (IDevice device : devices) {
			if (device instanceof IUsbSupport) {
				IUsbPort usb = ((IUsbSupport)device).getUsbPort();
				if (usb.hasConnectedDevice()) {
					IDevice usbDevice = usb.getConnectedDevice();
					
					if (usbDevice != null)
					System.out.println(usbDevice.getDeviceId() + " is connected to " + device.getDeviceId());
				}
			}
		}
		
		System.out.println("__________________________________");
		// register devices for network monitoring
		NetworkMonitor monitor = new NetworkMonitor();
		
		for (IDevice device : devices) {
			if (device instanceof INetworkAvailable) {
				monitor.registerDevice((INetworkAvailable)device);
			}
		}
		
		
		
		
		TrafficReportData reportData = monitor.getNetworkStatistics();
		
		TextReportBuilder text = new TextReportBuilder(reportData);
		String report = text.buildReport();
		
		System.out.println(report);
		
		System.out.println("__________________________________");
		// turn all devices off..
		for (IHomeDevice device : devices) {
			device.turnOff();
		}
	}

}
